import router from "./router";
import store from "@/store";
import userStore from "@/store/modules/user";
router.beforeEach((to, from, next) => {
  const token = store.getters.token;
  // const token = localStorage.getItem('blue')
  console.log(7, token);
  const url = to.path;
  const whiteList = ["/login", "/404", "/test"];
  const blackList = ["/realtime"];

  if (token) {
    // 判断有token去登录页跳转首页
    if (url === "/login") {
      next("/");
    } else {
      //判断逻辑解决刷新vuex数据丢失还有死循环
      // let  routerData = JSON.parse(localStorage.getItem("HSKJ-router-list"))
      // let routerState = router.options.routes.find(item => item.path === `/setting`)
      // if (routerState) {
      //   next()
      // }else{
      //   userStore.actions.vueXtokenLogin()
      //   console.log(27,router.options.routes);
      //   next(to.path)
      // }

      // 寻找路由判断
      let routerState = router.options.routes.find((item) => item.path === `*`);
      //长度判断
      // router.options.routes.length > 4

      //判断逻辑解决刷新vuex数据丢失还有死循环
      console.log(28, router.options.routes.length !== 0);
      if (routerState) {
        next();
      } else {
        userStore.actions.vueXtokenLogin();
        next(to.path);
      }

      //判断有token去其他页放行
      next();
    }
  } else {
    if (whiteList.indexOf(url) !== -1) {
      next();
    } else if (blackList.indexOf(url) !== -1) {
      //名单没有token不跳转 逻辑暂时没明白
      next("/404");
      return
    } else {
      next("/login");
      return
    }
  }
  // 其余放行
  next();
});
